<template>
  <div>
    <!-- 主题图片 -->
    <img-box
      width="100%"
      :height="200"
      :imgSrc="themePic"
    ></img-box>

    <!-- 内容区域 -->
    <div class="wrap xui-margin-bottom-xxl">
      <h3>找回密码</h3>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px" class="xui-padding-top-sm">
        <el-form-item label="账号" prop="userId">
          <el-input class="userId-input" style="width:210px;" v-model="form.userId" placeholder="请填写账号"></el-input>
          <el-button class="qrcode-btn" v-if="!isCount" type="primary" @click="getQrcode">获取验证码</el-button>
          <el-button class="qrcode-btn" v-if="isCount" type="primary" plain :disabled="true">{{timeout}}</el-button>
        </el-form-item>
        <el-form-item label="验证码" prop="qrcode">
          <el-input v-model="form.qrcode" placeholder="请填写您收到的验证码"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请设置新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="rePassword">
          <el-input v-model="form.rePassword" type="password" placeholder="请重新输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button class="submit-btn" type="primary" @click="onSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import imgBox from "@/components/widge/img-box";
import themePic from "@/assets/images/example/theme.png";
import { getQrcode, resetPassword } from "@/api/login";

export default {
  components: {
    "img-box": imgBox
  },
  data() {
    var validateRePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      themePic,
      timeout: 120,
      isCount: false,
      form: {
        userId: "",
        qrcode: "",
        password: "",
        rePassword: ""
      },
      rules: {
        userId: [
          { required: true, message: "请填写您的账号", trigger: "blur" }
        ],
        qrcode: [
          { required: true, message: "请输入手机收到的验证码", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请设置新密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "用户名长度为6～15之间",
            trigger: "blur"
          },
          {
            pattern: /^(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{6,15}$/,
            message: "密码为数字和英文的组合，不包含特殊字符，6-15位。",
            trigger: "blur"
          }
        ],
        rePassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validateRePass, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          resetPassword(
            this.form.userId,
            this.form.qrcode,
            this.form.password
          ).then(res => {
            console.log("adsfasdfa");
            if (res.flag == 1) {
              this.$message({
                showClose: true,
                type: "success",
                message: res.msg
              });
              this.$router.replace({ name: "home" });
              this.$store.dispatch("ToggleLoginDialog", true);
              return false;
            }
            if (res.flag == -1) {
              this.$message({
                showClose: true,
                type: "error",
                mesasge: res.msg
              });
            }
          });
        } else {
          this.$message({
            showClose: true,
            type: "error",
            message: "请按照规则填写信息"
          });
          return false;
        }
      });
    },
    getQrcode() {
      getQrcode(this.form.userId).then(res => {
        if (res.flag == 1) {
          let mobile = res.data.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
          this.$message({
            showClose: true,
            type: "success",
            message: `已发送验证码到手机${mobile},请注意查收`
          });
          this.countTimeout();
        }
        if (res.flag == -1) {
          this.$message({
            showClose: true,
            type: "error",
            message: res.msg
          });
        }
      });
    },
    countTimeout() {
      this.isCount = true;
      let timer = setInterval(() => {
        this.timeout -= 1;
        if (this.timeout == 0) {
          clearTimeout(timer);
          this.isCount = false;
          this.timeout = 120;
        }
      }, 1000);
    }
  }
};
</script>


<style lang="less" scoped>
.wrap {
  width: 1095px;
  margin: 0 auto 40px;
  h3 {
    color: #626262;
    border-bottom: 1px solid #e8e8e8;
    padding: 20px 0;
    text-align: center;
  }
  .el-form {
    width: 418px;
    margin: 0 auto;
  }
  span {
    display: block;
    text-align: center;
    margin-left: 54px;
  }
  .qrcode-btn {
    position: absolute;
    right: 0;
    top: 0;
  }
  .userId-inpu {
    width: 210px;
  }
}
.btn {
  text-align: center;
}
.el-button {
  display: inline-block;
}
.submit-btn {
  padding: 12px 30px;
}
</style>
